home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2267 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  52 lines

  1. Path: castle.nando.net!news
  2. From: actuary@nando.net   (Bill McCarthy)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Newbie Question - Probably Dumb as well
  5. Date: 20 Jan 1996 03:54:14 GMT
  6. Organization: News & Observer Public Access
  7. Message-ID: <4dpp16$n4s@castle.nando.net>
  8. References: <194990319wnr@csmltd.demon.co.uk>
  9. Reply-To: actuary@nando.net (Bill McCarthy)
  10. NNTP-Posting-Host: grail713.nando.net
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <194990319wnr@csmltd.demon.co.uk>, Martyn Read <Martyn@csmltd.demon.co.uk> writes:
  14. >Hello,
  15. >Apologies if this is a dumb question but:
  16. >I have a comma delimited file like
  17. >
  18. >999,"AAAAAAAAAA AAAAAAAAA",999,999,999,"AA"
  19. >Int, String               ,Int,Int,Int,String
  20. >
  21. >where all strings in the file have double quotes.
  22. >
  23. >Can I read the file into a struct using fscanf to ignore the double 
  24. >quotes as well as the commas?
  25.  
  26. If your string will always contain numbers and non-empty strings,
  27. then simply using fscanf(), or perhaps fgets() followed by sscanf(),
  28. is adequate.  For example:
  29.  
  30. #include <stdio.h>
  31.  
  32. int main( void )
  33. {
  34.    char  a[] = "999,\"AAAAAAAAAA AAAAAAAAA\",999,999,999,\"AA\"",
  35.          s1[100], s2[100];
  36.    int   i1, i2, i3, i4;
  37.  
  38.    sscanf( a, "%d,\"%[^\"]\",%d,%d,%d,\"%[^\"]\"",
  39.             &i1, s1, &i2, &i3, &i4, s2 );
  40.    printf( "%d %s %d %d %d %s\n", i1, s1, i2, i3, i4, s2 );
  41.  
  42.    return 0;
  43. }
  44.  
  45. To write a robust routing, you should write your own parsing
  46. function - checking all return values of functions and reporting
  47. the location and nature of the error on failure.
  48.  
  49. Bill McCarthy
  50. actuary@nando.net
  51. Wendell, NC  USA
  52.